package com.forezp.web;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

/**
 * Created by fangzhipeng on 2017/6/13.
 */
@RestController
public class RestTestController {

    @GetMapping("/testRest")
    public final String testRest() {
        final StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        final RestTemplate restTemplate = new RestTemplateBuilder().additionalMessageConverters(m).build();

//        final HttpHeaders headers = new HttpHeaders();
//        final MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//        headers.setContentType(type);
//        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

//        String jsonStr = JSONObject.toJSONString(params);
//        HttpEntity<String> formEntity = new HttpEntity<String>(jsonStr, headers);

        final String result = restTemplate.getForObject("http://www.baidu.com", String.class);
        return result;
    }
}
